package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
   @Autowired
   private MemberMapper memberMapper;
   @Autowired
   private OrderMapper orderMapper;
   @Autowired
   private SetmealMapper setmealMapper;

   /**
    * 运营统计
    * @return
    */
   public Map<String, Object> getBusinessReport() throws Exception {
      //当前日期字符串
      String reportDate = DateUtils.parseDate2String(new Date());
      //本周一日期字符串
      String mondayOfThisWeek = DateUtils.parseDate2String(DateUtils.getNDayOfThisWeek(1));
      //本月1号的日期字符串
      String firstDayOfThisMonth = DateUtils.parseDate2String(DateUtils.getNDayOfThisMonth(1));

      //新增会员数
      int todayNewMember = memberMapper.findMemberCountByDate(reportDate);
      //总会员数
      int totalMember = memberMapper.findCountAll();
      //本周新增会员数
      int thisWeekNewMember = memberMapper.findMemberCountAfterDate(mondayOfThisWeek);
      //本月新增会员数
      int thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDayOfThisMonth);

      //今日预约数
      int todayOrderNumber = orderMapper.findCountByDate(reportDate);
      //今日到诊数
      int todayVisitsNumber = orderMapper.findVisitsCountAfterDate(reportDate);
      //本周预约数
      int thisWeekOrderNumber = orderMapper.findOrderCountAfterDate(mondayOfThisWeek);
      //本周到诊数
      int thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate(mondayOfThisWeek);
      //本月预约数
      int thisMonthOrderNumber = orderMapper.findOrderCountAfterDate(firstDayOfThisMonth);
      //本月预约数
      int thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDayOfThisMonth);

      //运营数据统计
      List<Map<String,Object>> hotSetmeal = setmealMapper.findHostTop4Setmeal();

      Map<String, Object> result = new HashMap<>();
      result.put("reportDate", reportDate);
      result.put("todayNewMember", todayNewMember);
      result.put("totalMember", totalMember);
      result.put("thisWeekNewMember", thisWeekNewMember);
      result.put("thisMonthNewMember", thisMonthNewMember);
      result.put("todayOrderNumber", todayOrderNumber);
      result.put("todayVisitsNumber", todayVisitsNumber);
      result.put("thisWeekOrderNumber", thisWeekOrderNumber);
      result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
      result.put("thisMonthOrderNumber", thisMonthOrderNumber);
      result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
      result.put("hotSetmeal", hotSetmeal);
      return result;
   }
}
